﻿using NewsCollection.Common.Service;
using NewsCollection.Common.UpdateGUI;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsCollection.ListWeb
{
    class NewsZing
    {
        ProcessDb pdb = new ProcessDb();
        UpdateButton updateBtn = new UpdateButton();
        public void Run(ListView lv, Button btnStart, Button btnStop, int manuId,string folder)
        {

            Load load = new Load(lv);
            List<CategoryModel> ListCate = new List<CategoryModel>();

            string urlGoc = "http://news.zing.vn";


            ListCate = load.LoadCatesNoSub("http://news.zing.vn/thoi-trang.html", "//header//h2", urlGoc, manuId,"//a");


            int dem = 0;



            for (int i = 0; i < ListCate.Count(); i++)
            {
                // featured news

             
               
                // featured news


                var ListFeaturedNews = load.LoadUpdateNewHomeNews(ListCate[i], "//section[@class='featured']//article", "//header//h1//a", "", "//div[@class='cover']//a", urlGoc);
               //     var loadFeatured2 = load.LoadUpdateNewHomeNews(homeNow, "//div[@class='category-featured clearfix']//div[@class='more-topstory']//div[@class='story']", "//p[@class='title']//a", "", "//p[@class='photo']//a", urlGoc, "");
                  
              //      ListFeaturedNews.AddRange(loadFeatured2.ListNewsDetails);

                    for (int k = 0; k < ListFeaturedNews.Count(); k++)
                    {
                        var item = load.LoadNews(ListFeaturedNews[k], "//section[@id='content']//article", "//div[@class='summary']//p", manuId,0,"//div[@class='content']//img",urlGoc,true);
                        if (item != null)
                        {
                            dem++;
                            item.Hot = "Tin hot";
                            load.AddItemToListView(item);
                            pdb.SaveImgFeatured(folder, item);

                        }

                        //if (dem == 30)
                        //    break;
                    }
                
                
             dem +=   load.RecursionPageAndNews(ListCate[i], ListCate[i].Url, "//a[@id='ctl00_mainContent_ctl00_ContentList1_pager_nextControl']", "//article", "//header//h1//a",
                    "", "//header//h1//a", urlGoc, "//header//time", manuId, "//header//div[@class='summary']//p", "", "//article", "//div[@class='content']//img",folder);



              
            
           

            }
          //  System.Windows.Forms.MessageBox.Show("\nThanh cong!Co " + dem + " tin moi");

            updateBtn.UpdateButtonStartAfterRun(btnStart);
            updateBtn.UpdateButtonStopAfterRun(btnStop);
            //updateBtn.UpdateAfterRun(btnStart,btnStop);
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }
    }
}
